package com.alfredo.android.appcaja;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.alfredo.android.appcaja.constants.Keys;
import com.alfredo.android.appcaja.model.Orden;

public class DetalleOrdenActivity extends Activity 
{
	private Orden orden = null;
	private EditText etDetalleCgi;
	private EditText etDetalleNExpediente;
	private EditText etDetalleCausanteBeneficiario;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detalle_orden);
		
		etDetalleCgi = (EditText) findViewById(R.id.etDetalleCgi);
		etDetalleNExpediente = (EditText) findViewById(R.id.etDetalleNExpediente);
		etDetalleCausanteBeneficiario = (EditText) findViewById(R.id.etDetalleCausanteBeneficiario);
		
		Bundle extras = getIntent().getExtras();
				
		if(extras != null)
		{
			orden = (Orden) getIntent().getExtras().getSerializable(Keys.ORDEN);	
			etDetalleCgi.setText(String.valueOf(orden.getCgi()));
			etDetalleNExpediente.setText(String.valueOf(orden.getNroExpediente()));
			etDetalleCausanteBeneficiario.setText(String.valueOf(orden.getCausanteBeneficiario()));
		}
		else
		{
			etDetalleCgi.setEnabled(true);
			etDetalleNExpediente.setEnabled(true);
			etDetalleCausanteBeneficiario.setEnabled(true);
		}
		
		Button btnInformePreliminar = (Button) findViewById(R.id.btnInformePreliminar);
		btnInformePreliminar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{
				informePreliminar();
			}
		});
		
		Button btnInformeFinal = (Button) findViewById(R.id.btnInformeFinal);
		btnInformeFinal.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{
				informeFinal();
			}
		});
	}
	
	private void cargarNuevaOrden()
	{
		orden = new Orden();
		
		try
		{
			orden.setCgi(Long.parseLong(etDetalleCgi.getText().toString()));
			orden.setNroExpediente(etDetalleNExpediente.getText().toString());
			orden.setCausanteBeneficiario(etDetalleCausanteBeneficiario.getText().toString());
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private void informePreliminar()
	{
		if(orden == null)
			cargarNuevaOrden();
		
		Intent intent = new Intent(DetalleOrdenActivity.this, InformePreliminarActivity.class);
		intent.putExtra(Keys.ORDEN, orden);
		startActivity(intent);
	}
	
	private void informeFinal()
	{
		if(orden == null)
			cargarNuevaOrden();
		
		Intent intent = new Intent(DetalleOrdenActivity.this, InformeFinalActivity.class);
		intent.putExtra(Keys.ORDEN, orden);
		startActivity(intent);
	}
}
